package com.queclink.ycm.common.utils;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson.JSONObject;
import com.queclink.ycm.entity.Report;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jim.li
 * @email jim.li@queclink.com
 * @date 2024-07-24 15:40:00
 */
@Slf4j
@Component
public class CreateMessageUtil {


//    @PostConstruct
    public void init() {

        long startTime = System.currentTimeMillis();

        String fileName = "E://files//message.xlsx";

        String message = "{\"MessageType\":\"Report\",\"OffsetAddress\":0,\"RawDataLength\":99,\"Head\":{\"IMEI\":\"$imei$\",\"DeviceType\":\"5001\",\"ProtocolVersion\":27,\"CustomVersion\":0,\"TotalFrame\":0,\"CurrentFrame\":0},\"Body\":{\"Records\":[{\"GeneratedTime\":1721100383,\"RecordCountNumber\":0,\"recordLength\":76,\"ID\":\"F5\",\"EventCode\":0,\"DataIDs\":[{\"ID\":85,\"Data\":{\"PLMN\":\"0\",\"LAC\":\"3333\",\"CellID\":\"33333333\",\"Roaming\":0,\"AccessTechnology\":5,\"Band\":5,\"CSQ\":{\"RSSI\":60,\"BitErrorRate\":99}}},{\"ID\":88,\"Data\":{\"IMSI\":\"0460115896699853\",\"ICCID\":\"89860321840213172200\",\"SIMCardStatus\":0}},{\"ID\":90,\"Data\":{\"FirmwareVersion\":\"R00A01V02\"}}]}]},\"Tail\":{\"CountNumber\":291}}";
        // 创建输出目录

        ExcelWriter excelWriter = null;
        // 写入Excel文件
        try {

            excelWriter = EasyExcel.write(fileName).build();

            WriteSheet sheet1 = EasyExcel.writerSheet(0, "Sheet1").build();
            WriteSheet sheet2 = EasyExcel.writerSheet(1, "Sheet2").build();
            WriteSheet sheet3 = EasyExcel.writerSheet(2, "Sheet3").build();
            WriteSheet sheet4 = EasyExcel.writerSheet(3, "Sheet4").build();

            excelWriter.write(createMessage(0, message), sheet1);
            excelWriter.write(createMessage(25000, message), sheet2);
            excelWriter.write(createMessage(50000, message), sheet3);
            excelWriter.write(createMessage(75000, message), sheet4);
            // 写入到文件
//            EasyExcel.write(filePath, DataModel.class)
//                    .sheet(0).sheetName("Sheet1")
//                    .doWrite(createMessage(0, message));
//
//            EasyExcel.write(filePath, DataModel.class)
//                    .sheet(1).sheetName("Sheet2")
//                    .doWrite(createMessage(25000, message));
//
//            EasyExcel.write(filePath, DataModel.class)
//                    .sheet(2).sheetName("Sheet3")
//                    .doWrite(createMessage(50000, message));
//
//            EasyExcel.write(filePath, DataModel.class)
//                    .sheet(3).sheetName("Sheet4")
//                    .doWrite(createMessage(75000, message));


        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (excelWriter != null) {
                excelWriter.finish();
            }
        }
        long endTime = System.currentTimeMillis();
        long usageTime = endTime - startTime;
        log.info("接口使用时间：" + usageTime + "毫秒");
    }

    public List<DataModel> createMessage(int startIndex, String message) {
        List<DataModel> data = new ArrayList<>();
        try {
            for (int i = startIndex; i < startIndex + 25000; i++) {
                DataModel dataModel = new DataModel();
                String numStr = "500100000" + String.format("%06d", i);
                // 创建数据
                String msg = message.replace("$imei$", numStr);
                JSONObject jsonObject = JSONObject.parseObject(msg);
                Report report = new Report();
                String combine = report.combine(jsonObject);
                dataModel.setMessage(combine);
                data.add(dataModel);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }

}
